import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import moment from 'moment';
import { useEffect, useRef, useState } from 'react';
import { getServerTime } from '@/recoil/productionBroad/api';
import styles from './boardHead.module.less';
export default (props) => {
    const { title } = props;
    const timmer = useRef();
    const timeRef = useRef();
    const [time, setTime] = useState(0);
    const [currentTime, setCurrentTime] = useState(moment().format('YYYY.MM.DD HH:mm'));
    const getWeek = () => {
        return ('星期' +
            ['日', '一', '二', '三', '四', '五', '六'][moment(currentTime).weekday()]);
    };
    const getTime2 = () => {
        try {
            clearInterval(timeRef.current);
            const now = Date.now();
            setTime(+moment(now).format('ss'));
            const current = moment(now).format('YYYY.MM.DD HH:mm');
            setCurrentTime(current);
        }
        catch (err) {
            console.log(err);
        }
    };
    const getTime = async () => {
        try {
            const res = await getServerTime();
            clearInterval(timeRef.current);
            setTime(+moment(res.data).format('ss'));
            const current = moment(res.data).format('YYYY.MM.DD HH:mm');
            setCurrentTime(current);
        }
        catch (err) {
            console.log(err);
        }
    };
    useEffect(() => {
        const t = setTimeout(() => {
            getTime2();
        }, 1000);
        return () => {
            clearInterval(t);
        };
    }, [time]);
    useEffect(() => {
        // clearInterval(timeRef.current)
        // timeRef.current = setInterval(() => {
        //   const nextTime = time + 1
        //   if (nextTime >= 60) {
        //     getTime()
        //     return
        //   }
        //   setTime(nextTime)
        // }, 1000)
    }, [time]);
    useEffect(() => {
        // getTime()
        // timmer.current = setInterval(getTime, 30000)
        // return () => {
        //   clearTimeout(timmer.current)
        // }
    }, []);
    return (_jsx("div", { className: styles.container, children: _jsxs("div", { className: styles.headBg, children: [_jsx("div", { className: styles.headerTitle2, children: title }), _jsxs("div", { className: styles.time2, children: [_jsx("span", { className: styles.timeSpan2, children: currentTime }), _jsx("span", { className: styles.timeSpan2, children: getWeek() })] })] }) }));
};
